package com.ydgk.service;

import com.ydgk.bean.Pclass;
import com.ydgk.bean.Product;
import com.ydgk.dao.ProductDao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProductService {
    ProductDao productDao=new ProductDao();
    public Map AdminSearch(String productName, Integer pageNumber){
        int maxPage = productDao.adminSearchMaxPage(productName);
        if(pageNumber > maxPage){
            pageNumber = maxPage;
        }
        if(pageNumber < 1){
            pageNumber = 1;
        }
        String pageCode = productDao.genPagination
                ("ProductServlet.action",maxPage,pageNumber,"flag=findProduct&productName="+(productName==null?"":productName));
        List<Product> productList = productDao.adminSearch(productName, pageNumber);
        HashMap map = new HashMap();
        map.put("pageCode",pageCode);
        map.put("productList",productList);
        return map;
    }
    public int saveOrUpdate(Product product){

        if(product.getId() != null && product.getId() != 0 ){
            //修改
            return productDao.updateById(product);
        }else{
            //添加
            return productDao.save(product);
        }
    }
    public int deleteById(int id){
        return productDao.deleteById(id);
    }
}
